﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions campus
        ////////////////////////FONCTIONS CAMPUS////////////////////////////////


        //Listing des campus enregistrés
        private void list_campus_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            if (usertype == "administrators")
            {

                foreach (Campus s in (from e in db_local.Campus select e))
                {
                    maListe.Items.Add(s.Name);

                }
            }

            else if (usertype == "managers")
            {
                foreach (Campus s in (from e in db_local.Campus
                                      where e.Name == usercampus
                                      select e))
                {
                    maListe.Items.Add(s.Name);

                }
            }

        }


        //Affichage de la liste des campus
        private void display_campus_offline(String campus)
        {
            campus_current_period_listbox.Items.Clear();

            foreach (Campus s in (from e in db_local.Campus select e))
            {
                if (s.Name == campus)
                {
                    campus_name_textbox.Text = s.Name;
                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_campus_offline()
        {
            campus_name_textbox.Text = "";
            list_campus_offline(campus_listbox);
            list_campus_offline(campus_listbox2);
            chargement_liste_campus_manager_offline();
            chargement_liste_campus_student_offline();

            campus_current_period_listbox.Items.Clear();



        }


        //Ajout d'un campus
        private void add_campus_offline()
        {
            Campus campus1 = new Campus();

            campus1.Name = campus_name_textbox.Text.ToLower();

            db_local.Campus.InsertOnSubmit(campus1);
            db_local.SubmitChanges();


            //Rafraichissement des éléments
            refresh_campus_offline();


        }


        //Mise à jour d'un campus
        private void update_campus_offline()
        {
            if (campus_name_textbox.Text != "")
            {
                Campus campus2 = (from e in db_local.Campus
                                  where e.Name == (string)campus_listbox.SelectedValue
                                  select e).FirstOrDefault();

                campus2.Name = campus_name_textbox.Text.ToLower();

                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_campus_offline();
            }

        }


        //Suppression d'un campus
        private void delete_campus_offline()
        {
            if (campus_name_textbox.Text != "")
            {
                Campus campus3 = (from e in db_local.Campus
                                  where e.Name == (string)campus_listbox.SelectedValue
                                  select e).FirstOrDefault();

                db_local.Campus.DeleteOnSubmit(campus3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_campus_offline();
            }

        }

        private void set_periods_offline()
        {
            //On définit l'id du campus sélectionné
            int current_campus_id = (from e in db_local.Campus
                                     where e.Name == campus_listbox.SelectedValue.ToString()
                                     select e.Id).FirstOrDefault();

            //On définit l'id du cursus sélectionné
            int current_cursus_id = (from e in db_local.Cursus
                                     where e.Name == campus_cursus_combobox.SelectedValue.ToString()
                                     select e.Id).FirstOrDefault();


            //On supprime les entrées existantes pour un campus donné avant de rajouter celles sélectionnées
            foreach (Period_campus s in (from e in db_local.Period_campus select e))
            {
                if (s.Campus_id == current_campus_id)
                {
                    int period_cursus_id_temp = (from e in db_local.Period
                                                 where e.Id == s.Period_id
                                                 select e.Cursus_id).FirstOrDefault();

                    if (period_cursus_id_temp == current_cursus_id)
                    {
                        db_local.Period_campus.DeleteOnSubmit(s);
                    }
                }
            }

            db_local.SubmitChanges();


            //On rajoute les entrées sélectionnées

            for (int i = 0; i < campus_period_listbox.SelectedItems.Count; i++)
            {

                Period_campus period1 = new Period_campus();

                //On définit l'id de la periode sélectionnée
                int current_period_id = (from e in db_local.Period
                                         where e.Name_period == current_cursus_id + "?" + campus_period_listbox.SelectedItems[i].ToString()
                                         select e.Id).FirstOrDefault();

                period1.Period_id = current_period_id;

                //On attribue l'id du campus concerné
                period1.Campus_id = current_campus_id;

                //On ajoute dans la base de données
                db_local.Period_campus.InsertOnSubmit(period1);
                db_local.SubmitChanges();

            }

            campus_current_period_listbox.Items.Clear();

        }


        private void chargement_liste_cursus_campus_offline()
        {
            campus_cursus_combobox.Items.Clear();

            foreach (Cursus s in (from e in db_local.Cursus select e))
            {
                campus_cursus_combobox.Items.Add(s.Name);
            }
        }

        private void chargement_liste_period_campus_offline()
        {
            if (campus_name_textbox.Text != "")
            {

                int id_cursus_temp = (from i in db_local.Cursus
                                      where i.Name.Equals(campus_cursus_combobox.SelectedValue.ToString())
                                      select i.Id).FirstOrDefault();

                campus_period_listbox.Items.Clear();

                //On affiche la liste de toutes les périodes selon le cursus choisi
                foreach (Period s in (from e in db_local.Period where e.Cursus_id == id_cursus_temp select e))
                {
                    string period_name_temp = s.Name_period;
                    string[] period_name = period_name_temp.Split('?');

                    campus_period_listbox.Items.Add(period_name[1]);
                }




                //On affiche la liste des périodes déjà attribuées selon le cursus choisi
                int current_campus_id = (from e in db_local.Campus
                                         where e.Name == campus_listbox.SelectedValue.ToString()
                                         select e.Id).FirstOrDefault();

                campus_current_period_listbox.Items.Clear();

                foreach (Period_campus j in (from e in db_local.Period_campus where e.Campus_id == current_campus_id select e))
                {
                    if (j.Id != 0)
                    {

                        string current_period_name = (from e in db_local.Period
                                                      where e.Id == j.Period_id
                                                      select e.Name_period).FirstOrDefault();

                        int current_cursus_id = (from e in db_local.Period
                                                 where e.Id == j.Period_id
                                                 select e.Cursus_id).FirstOrDefault();

                        string current_cursus_name = (from e in db_local.Cursus
                                                      where e.Id == current_cursus_id
                                                      select e.Name).FirstOrDefault();


                        string[] period_name = current_period_name.Split('?');

                        if (current_cursus_name == campus_cursus_combobox.SelectedValue.ToString())
                        {
                            campus_current_period_listbox.Items.Add(period_name[1] + " (" + current_cursus_name + ")");
                        }
                    }
                }
            }

            else
            {
                MessageBox.Show("Please select a campus first.");
            }




        }

        #endregion
    }
}
